package org.xielei.stream;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Sort
 *
 * @author xielei
 * @date 2020/12/3
 */
public class SortTest {

    public static void main(String[] args) {

        List<WorldGroup> worldGroupList = new ArrayList<>(Arrays.asList(
                new WorldGroup("1", "1", null, new Date()),
                new WorldGroup("1", "2", 1, null),
                new WorldGroup("1", "3", 3, new Date(System.currentTimeMillis() - 10000L)),
                new WorldGroup("1", "4", null, null)));

        List<WorldGroup> collect = worldGroupList.stream()
                .sorted((o1, o2) -> comparator(o1, o2))
                .map(worldGroup -> buildNullDate(worldGroup))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        System.out.println("sort:" + collect.toString());

        String consultNum = Optional.ofNullable(worldGroupList)
                .filter(list -> !CollectionUtils.isEmpty(list))
                .map(list -> list.get(0))
                .map(WorldGroup::getConsultSum)
                .map(Object::toString)
                .orElse("0");
        System.out.println("------>>>: " + consultNum);
    }

    private static WorldGroup buildNullDate(WorldGroup worldGroup) {
        return null;
    }

    private static int comparator(WorldGroup o1, WorldGroup o2) {
        if (o1.getDate() == null && o2.getDate() == null) {
            return 0;
        }
        if (o1.getDate() == null && o2.getDate() != null) {
            return 1;
        }
        if (o1.getDate() != null && o2.getDate() == null) {
            return -1;
        }

        return Objects.requireNonNull(o1.getDate()).compareTo(o2.getDate());
    }

    private static int comparatorInteger(WorldGroup o1, WorldGroup o2) {
        if (o1.consultSum == null && o2.consultSum != null) {
            return 1;
        }
        if (o1.consultSum !=  null && o2.consultSum == null) {
            return -1;
        }
        if (o1.consultSum == null && o2.consultSum == null) {
            return 0;
        }

        return (o1.consultSum < o2.consultSum) ? -1 : ((o1.consultSum.equals(o2.consultSum)) ? 0 : 1);
    }

    @Test
    public void sortTest() {
        String collect = Stream.of("00000b4219", "56345", 357)
                .map(Object::toString)
                .collect(Collectors.joining(","));

        System.out.println(collect);
    }

    @Getter
    @Setter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    private static class WorldGroup {

        private String ifRecommend;

        private String id;

        private Integer consultSum;

        private Date date;
    }
}
